package org.springblade.modules.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springblade.modules.platform.dto.ProductModelFunctionDTO;
import org.springblade.modules.platform.dto.ProductModelLabelDTO;
import org.springblade.modules.platform.dto.ProductModelPropertiesDTO;
import org.springblade.modules.platform.dto.ProductModelPropertiesEventsDTO;
import org.springblade.modules.platform.entity.*;
import org.springblade.modules.platform.service.*;
import org.springblade.modules.platform.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.tool.api.R;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RestController
@AllArgsConstructor
@RequestMapping("/service-huateng-sxdb/objectmodel")
@Api(value = "", tags = "物模型接口")
@Slf4j
public class ObjectModelController extends BladeController {

	private final IProductModelFunctionService productModelFunctionService;
	private final IProductModelPropertiesService productModelPropertiesService;
	private final IProductModelPropertiesEventsService productModelPropertiesEventsService;
	private final IProductModelLabelService productModelLabelService;



	/**
	 * 详情
	 */
	@ApiLog("产品-物模型-功能定义")
	@GetMapping("/productmodelfunction/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "物模型-功能定义", notes = "传入产品编号")
	public R<ProductModelFunctionVO> productModelProductmodelfunction(String productId) {
		ProductModelFunction byId = productModelFunctionService.getOne(new QueryWrapper<ProductModelFunction>().lambda().eq(ProductModelFunction::getProductId,productId));
		ProductModelFunctionVO productModelFunctionVO = new ProductModelFunctionVO();
		if(byId==null){
			return R.data(productModelFunctionVO);
		}
		BeanUtils.copyProperties(byId,productModelFunctionVO);
		return R.data(productModelFunctionVO);
	}
	/**
	 * 新增
	 */
	@ApiLog("产品-物模型-功能定义-新增")
	@PostMapping("/productmodelfunction/save")
	@ApiOperationSupport(order =2)
	@ApiOperation(value = "物模型-功能定义-新增", notes = "传入productModelProperties")
	public R saveProductmodelfunction(@Valid @RequestBody ProductModelFunctionDTO productModelFunctionDTO) {
		List<ProductModelFunction> list = productModelFunctionService.list(new QueryWrapper<ProductModelFunction>().lambda()
				.eq(ProductModelFunction::getProductId, productModelFunctionDTO.getProductId()));
		if(!CollectionUtils.isEmpty(list)){
			list.forEach(x-> productModelFunctionService.removeById(x.getId()));

		}

		ProductModelFunction productModelFunction = new ProductModelFunction();
		BeanUtils.copyProperties(productModelFunctionDTO,productModelFunction);
		productModelFunction.setUpdateTime(System.currentTimeMillis());
		productModelFunction.setInsertTime(System.currentTimeMillis());
		return R.status(productModelFunctionService.save(productModelFunction));
	}

	/**
	 * 修改modelproperties/update
	 */
	@ApiLog("产品-物模型-功能定义-修改")
	@PostMapping("/productmodelfunction/update")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "物模型-功能定义-修改", notes = "传入productModelProperties")
	public R updateProductmodelfunction(@Valid @RequestBody ProductModelFunctionDTO productModelFunctionDTO) {
		ProductModelFunction productModelFunction = new ProductModelFunction();
		BeanUtils.copyProperties(productModelFunctionDTO,productModelFunction);
		productModelFunction.setUpdateTime(System.currentTimeMillis());
		productModelFunctionService.updateById(productModelFunction);
		return R.status(true);
	}

	@ApiLog("产品-物模型-属性定义")
	@GetMapping("/modelproperties/detail")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "物模型-属性定义", notes = "传入产品编号")
	public R<ProductModelPropertiesVO> detail(String productId) {
		ProductModelPropertiesVO productModelPropertiesVO = new ProductModelPropertiesVO();
		ProductModelProperties byId = productModelPropertiesService.getOne(new QueryWrapper<ProductModelProperties>().lambda().eq(ProductModelProperties::getProductId,productId));
		if(byId != null){
			BeanUtils.copyProperties(byId,productModelPropertiesVO);
		}
		return R.data(productModelPropertiesVO);
	}

	@ApiLog("产品-物模型-属性定义")
	@PostMapping("/modelproperties/add")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "物模型-属性定义-添加", notes = "传入产品编号")
	public R detail(@Valid @RequestBody ProductModelPropertiesDTO productModelPropertiesDTO) {
		List<ProductModelProperties> list = productModelPropertiesService.list(new QueryWrapper<ProductModelProperties>().lambda().eq(ProductModelProperties::getProductId, productModelPropertiesDTO.getProductId()));
		if(!CollectionUtils.isEmpty(list)){
			list.forEach(x-> productModelPropertiesService.removeById(x.getId()));
		}
		ProductModelProperties productModelFunction = new ProductModelProperties();
		BeanUtils.copyProperties(productModelPropertiesDTO,productModelFunction);
		productModelFunction.setUpdateTime(System.currentTimeMillis());
		productModelFunction.setInsertTime(System.currentTimeMillis());
		return R.data(productModelPropertiesService.save(productModelPropertiesDTO));
	}
	/**
	 * 修改
	 */
	@ApiLog("产品-物模型-属性定义-修改")
	@PostMapping("/modelproperties/update")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "物模型-属性-修改", notes = "传入productModelPropertiesDTO")
	public R updateProductmodelmodelproperties(@Valid @RequestBody ProductModelPropertiesDTO productModelPropertiesDTO) {
		ProductModelProperties productModelProperties = new ProductModelProperties();
		BeanUtils.copyProperties(productModelPropertiesDTO,productModelProperties);
		productModelProperties.setUpdateTime(System.currentTimeMillis());
		productModelProperties.setUpdateTime(System.currentTimeMillis());
		productModelPropertiesService.updateById(productModelProperties);
		return R.status(true);
	}

	@ApiLog("产品-物模型-事件定义")
	@GetMapping("/modelPropertiesEvents/detail")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "物模型-事件定义", notes = "传入productModelProperties")
	public R<ProductModelPropertiesEventsVO> modelPropertiesEventsdetail(String productId) {
		ProductModelPropertiesEvents one = productModelPropertiesEventsService.getOne(new QueryWrapper<ProductModelPropertiesEvents>().lambda().eq(ProductModelPropertiesEvents::getProductId, productId));
		ProductModelPropertiesEventsVO productModelPropertiesEventsVO = new ProductModelPropertiesEventsVO();
		if(one!=null){
			BeanUtils.copyProperties(one,productModelPropertiesEventsVO);
		}

		return R.data(productModelPropertiesEventsVO);
	}
	@ApiLog("产品-物模型-事件定义")
	@PostMapping("/modelPropertiesEvents/add")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "物模型-事件定义-新增", notes = "传入productModelProperties")
	public R modelPropertiesEventsadd(@Valid @RequestBody ProductModelPropertiesEventsDTO productModelPropertiesEventsDTO) {
		List<ProductModelPropertiesEvents> list = productModelPropertiesEventsService.list(new QueryWrapper<ProductModelPropertiesEvents>().lambda().eq(ProductModelPropertiesEvents::getProductId, productModelPropertiesEventsDTO.getProductId()));
		if(!CollectionUtils.isEmpty(list)){
			if(!CollectionUtils.isEmpty(list)){
				list.forEach(x-> productModelPropertiesEventsService.removeById(x.getId()));
			}
		}

		ProductModelPropertiesEvents events = new ProductModelPropertiesEvents();
		BeanUtils.copyProperties(productModelPropertiesEventsDTO,events);
		events.setUpdateTime(System.currentTimeMillis());
		events.setInsertTime(System.currentTimeMillis());
		return R.data(productModelPropertiesEventsService.save(events));
	}
	/**
	 * 修改
	 */
	@ApiLog("产品-物模型-事件定义-修改")
	@PostMapping("/modelPropertiesEvents/update")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "产品-物模型-事件定义-修改", notes = "传入productModelPropertiesEventsDTO")
	public R modelPropertiesEventsUpdate(@Valid @RequestBody ProductModelPropertiesEventsDTO productModelPropertiesEventsDTO) {
		ProductModelPropertiesEvents productModelFunction = new ProductModelPropertiesEvents();
		BeanUtils.copyProperties(productModelPropertiesEventsDTO,productModelFunction);
		productModelFunction.setUpdateTime(System.currentTimeMillis());
		productModelPropertiesEventsService.updateById(productModelFunction);
		return R.status(true);
	}
	/**
	 * 新增
	 */
	@ApiLog("产品-物模型-标签")
	@PostMapping("/modellabe/add")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "物模型-标签-新增", notes = "传入productModelLabel")
	public R saveModelLabel(@Valid @RequestBody ProductModelLabelDTO productModelLabelDTO) {
		List<ProductModelLabel> list = productModelLabelService.list(new QueryWrapper<ProductModelLabel>().lambda().eq(ProductModelLabel::getProductId, productModelLabelDTO.getProductId()));
		if(!CollectionUtils.isEmpty(list)){
			return R.fail("产品物模型已存在");
		}
		productModelLabelDTO.setInsertTime(System.currentTimeMillis());
		productModelLabelDTO.setUpdateTime(System.currentTimeMillis());
		ProductModelLabel productModelLabel = new ProductModelLabel();
		BeanUtils.copyProperties(productModelLabelDTO,productModelLabel);
		return R.status(productModelLabelService.save(productModelLabel));
	}
	/**
	 * 修改
	 */
	@ApiLog("产品-物模型-标签-修改")
	@PostMapping("/modellabe/update")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "物模型-标签-修改", notes = "传入productModelLabel")
	public R updateModelLabel(@Valid @RequestBody ProductModelLabelDTO productModelLabelDTO) {
		productModelLabelDTO.setUpdateTime(System.currentTimeMillis());
		ProductModelLabel productModelLabel = new ProductModelLabel();
		BeanUtils.copyProperties(productModelLabelDTO,productModelLabel);
		return R.status(productModelLabelService.updateById(productModelLabel));
	}

	@ApiLog("产品-物模型-标签")
	@GetMapping("/modellabe/detail")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "物模型-标签", notes = "传入ids")
	public R<ProductModelLabelVO> detailModelLabe(String productId) {
		ProductModelLabel one = productModelLabelService.getOne(new QueryWrapper<ProductModelLabel>().lambda().eq(ProductModelLabel::getProductId, productId));
		ProductModelLabelVO productModelLabelVO = new ProductModelLabelVO();
		if(one==null){
			return R.data(productModelLabelVO);
		}
		BeanUtils.copyProperties(one,productModelLabelVO);
		return R.data(productModelLabelVO);
	}
	/**
	 * 分页
	 */
	@ApiLog("产品-产品告警-获取属性下拉框")
	@GetMapping("/alarm/productModelPropertieslist")
	@ApiOperationSupport(order = 18)
	@ApiOperation(value = "产品告警-获取属性下拉框", notes = "传入productId")
	public R<ProductModelPropertiesVO> productModelPropertieslist(String productId) {
		ProductModelProperties one = productModelPropertiesService.getOne(new QueryWrapper<ProductModelProperties>().lambda().eq(ProductModelProperties::getProductId, productId));
		ProductModelPropertiesVO productModelPropertiesVO = new ProductModelPropertiesVO();
		BeanUtils.copyProperties(one,productModelPropertiesVO);
		return R.data(productModelPropertiesVO);
	}
	/**
	 * 分页
	 */
	@ApiLog("产品-产品告警-获取事件下拉框")
	@GetMapping("/alarm/productModelPropertiesEventslist")
	@ApiOperationSupport(order = 33)
	@ApiOperation(value = "产品告警-获取事件下拉框", notes = "传入productId")
	public R<ProductModelPropertiesEventsVO> productModelPropertiesEventslist(String productId) {
		ProductModelPropertiesEvents one = productModelPropertiesEventsService.getOne(new QueryWrapper<ProductModelPropertiesEvents>().lambda().eq(ProductModelPropertiesEvents::getProductId, productId));
		ProductModelPropertiesEventsVO productModelPropertiesVO = new ProductModelPropertiesEventsVO();
		BeanUtils.copyProperties(one,productModelPropertiesVO);
		return R.data(productModelPropertiesVO);
	}


}
